var pdtBody = $("#pdtBody");
var clearmodel = false;
$(function(){	 
	 initToolbar();	 
});

function ajaxSubmit(ns) {

	var postdata = $("form").serialize();
	var dataurl = ns + ((rptId==''||rptId==null||rptId=='null')?'create':'update');
	$.post(dataurl,postdata,function(data){
		if(data=="" || data==null) return;
		var url = ns+'get?bean.id='+data;
		window.open(url, '_self');
	});


}

function subData(formid) {
	try {
		var checked = confirm("您确认保存当前更改?");
		// testForm();
		if (checked) {
			ajaxSubmit(ns);
		}
	} catch (e) {
		alert(e.message);
	}
}

function initToolbar() {
	
	$("#datepicker").datepicker({dateFormat : 'yy-mm-dd'});	
	$("#rbtn").bind("click",function() {				
		var url = 'viewattach?id=' + rptId + '&model=0&clearmodel='	+ clearmodel;
		window.open(url, '_docwin');
	});
	
	$("#saveBtn").bind("click",function() {
		subData('armform');
	});
	$("#newPdt").bind("click",showAddDialog);
	$("#downloadBtn").bind("click",download);
}
function setCleanModel(obj){	
	clearmodel = obj.checked;	
}
function testForm() {
	var form = document.getElementById('armform');
	var msg = '';
	for ( var i = 0; i < form.elements.length; i++) {
		msg += form.elements[i].name + ' = ' + form.elements[i].value + '<br/>';
	}
	$("#debug").html(msg);

}
function test() {
	testForm();
}

/**
 * 为该表格容器添加表格行
 * @param datas 数据对象数组,包括三个域 type,events,attrs
 * @param container 容器编号
 * 
 * */
function fieldValueName(formName, index,valueName) {
	var dataPrefix = "bean.armItems[" + index + "].dataItems['";
	var dataPostfix = "'].";
	return dataPrefix + formName + dataPostfix + valueName;
}
function addDataItem(datas,container){
	var contar =container;
	var tr = $("<tr></tr>");
	contar.append(tr);
	$.each(datas,function(i,data){
		var e = elementByType(data.type);
		e.attr(data.attrs||{});
		
		e.css(data.csses||{});
	    e.bind(data.events||{});
	    e.addClass(data.className ||"");
	   
	    if(data.type=="label"){
	    	e.html(data.attrs.text);
	    }
	    if(data.type=='hidden'){
	    	contar.append(e);
	    	return ;
	    }
	    var td = $("<td></td>");
	    td.append(e);
	    tr.append(td);
	});
	$('.remarkTip').tooltip();
}

function setAutoComplete(objId) {
    var obj = $(objId);
	obj.autocomplete({
		minLength : 3,
		source : function(request, response) {
			var term = request.term;
			$.getJSON(
					"../../product/product!searchall", 
					{"term" : term}, 
					function(data) {
						response(data);
					}							
			);
		},
		focus : function(event, ui) {
			obj.val(ui.item.code);
			return false;
		},
		select : function(event, ui) {
			obj.val(ui.item.code);		   
			return false;
		}

	}).data("autocomplete")._renderItem = function(ul, item) {
		return $("<li></li>").data("item.autocomplete", item).append(
				"<a>" + item.code + "|" + item.name + "</a>").appendTo(ul);
	};
}
function closeMe() {
	window.opener = null;
	window.close();
}
function download(){
	var url=ns+"download?bean.id="+rptId;
	window.open(url,"_downwin");
}

function removeLine(self){
	var tr = $(self).parent().parent();
	tr.remove();
}